package com.burst.reptile.modular.api.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.burst.reptile.common.constant.UserConstants;
import com.burst.reptile.common.utils.poi.ExcelUtil;
import com.burst.reptile.framework.aspectj.lang.annotation.Log;
import com.burst.reptile.framework.aspectj.lang.enums.BusinessType;
import com.burst.reptile.framework.web.controller.BaseController;
import com.burst.reptile.framework.web.result.LayuiResult;
import com.burst.reptile.framework.web.result.Result;
import com.burst.reptile.modular.zp.entity.ZpServiceItems;
import com.burst.reptile.modular.zp.service.IZpServiceItemsService;

import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 服务项目列表 前端控制器
 * </p>
 *
 * @author wujin
 * @since 2020-06-23
 */
@RestController
@RequestMapping("/zp/service")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ZpServiceItemsController extends BaseController{

	private final IZpServiceItemsService serviceItemsService;
	
	/**
	 * 获取所有服务项目
	 * @return
	 */
	@GetMapping("/all")
	public Result<Object> all(){
		return Result.success(serviceItemsService.list());
	}
	
	/**
	 * 获取所有服务项目
	 * @return
	 */
	@GetMapping("/list")
	@PreAuthorize("@ss.hasPermi('zp:service:list')")
	public LayuiResult<Object> list(ZpServiceItems serviceItems){
		List<ZpServiceItems> list = serviceItemsService.listAll(serviceItems);
		return LayuiResult.success(list.size(), list);
	}
	
	/**
	 * 新增
	 * @return
	 */
	@PostMapping("/add")
	@PreAuthorize("@ss.hasPermi('zp:service:add')")
	@Log(title = "服务管理", businessType = BusinessType.INSERT)
	public Result<Object> add(@Validated @RequestBody ZpServiceItems serviceItems){
		if (UserConstants.NOT_UNIQUE.equals(serviceItemsService.checkServiceItemsKeyUnique(serviceItems))) {
			return Result.error("新增服务项目'" + serviceItems.getName() + "'失败，已存在，不能重复添加");
		}
		return Result.success(serviceItemsService.insertServiceItems(serviceItems));
	}
	
	/**
	 * 编辑
	 * @return
	 */
	@PutMapping("/edit")
	@PreAuthorize("@ss.hasPermi('zp:service:edit')")
	@Log(title = "服务管理", businessType = BusinessType.UPDATE)
	public Result<Object> edit(@Validated @RequestBody ZpServiceItems serviceItems){
		if (UserConstants.NOT_UNIQUE.equals(serviceItemsService.checkServiceItemsKeyUnique(serviceItems))) {
			return Result.error("服务项目'" + serviceItems.getName() + "'已存在");
		}
		return Result.success(serviceItemsService.updateServiceItems(serviceItems));
	}
	
	/**
	 * 删除
	 * @return
	 */
	@DeleteMapping("/remove/{ids}")
	@PreAuthorize("@ss.hasPermi('zp:service:remove')")
	@Log(title = "服务管理", businessType = BusinessType.DELETE)
	public Result<Object> remove(@PathVariable Integer[] ids){
		return Result.success(serviceItemsService.deleteByIds(ids));
	}
	
	/**
	 * 导出
	 * @return
	 */
	@GetMapping("/export")
	@PreAuthorize("@ss.hasPermi('zp:service:export')")
	public Result<Object>export(){
		List<ZpServiceItems> list = serviceItemsService.list();
		ExcelUtil<ZpServiceItems> util = new ExcelUtil<>(ZpServiceItems.class);
		return util.exportExcel(list, "业态数据");
	}
	
}
